1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package com.google.common.cache;
16
17 import static com.google.common.base.Preconditions.checkNotNull;
18
19 import com.google.common.annotations.GwtCompatible;
20 import com.google.common.annotations.GwtIncompatible;
21 import com.google.common.collect.Maps;
22 import com.google.common.util.concurrent.Futures;
23 import com.google.common.util.concurrent.ListenableFuture;
24
25 import java.util.Map;
26 import java.util.concurrent.atomic.AtomicInteger;
27
28 import javax.annotation.Nullable;
29
30
31
32
33
34
35 @GwtCompatible(emulated = true)
36 class TestingCacheLoaders {
37
38
39
40
41
42 static <K, V> CacheLoader<K, V> bulkLoader(final CacheLoader<K, V> loader) {
43 checkNotNull(loader);
44 return new CacheLoader<K, V>() {
45 @Override
46 public V load(K key) throws Exception {
47 return loader.load(key);
48 }
49
50 @Override
51 public Map<K, V> loadAll(Iterable<? extends K> keys) throws Exception {
52 Map<K, V> result = Maps.newHashMap();
53 for (K key : keys) {
54 result.put(key, load(key));
55 }
56 return result;
57 }
58 };
59 }
60
61
62
63
64 static <K, V> ConstantLoader<K, V> constantLoader(@Nullable V constant) {
65 return new ConstantLoader<K, V>(constant);
66 }
67
68
69
70
71 static IncrementingLoader incrementingLoader() {
72 return new IncrementingLoader();
73 }
74
75
76
77
78 static <K, V> CacheLoader<K, V> errorLoader(final Error e) {
79 checkNotNull(e);
80 return new CacheLoader<K, V>() {
81 @Override
82 public V load(K key) {
83 throw e;
84 }
85 };
86 }
87
88
89
90
91 static <K, V> CacheLoader<K, V> exceptionLoader(final Exception e) {
92 checkNotNull(e);
93 return new CacheLoader<K, V>() {
94 @Override
95 public V load(K key) throws Exception {
96 throw e;
97 }
98 };
99 }
100
101
102
103
104 static <T> IdentityLoader<T> identityLoader() {
105 return new IdentityLoader<T>();
106 }
107
108
109
110
111
112 static class CountingLoader extends CacheLoader<Object, Object> {
113 private final AtomicInteger count = new AtomicInteger();
114
115 @Override
116 public Object load(Object from) {
117 count.incrementAndGet();
118 return new Object();
119 }
120
121 public int getCount() {
122 return count.get();
123 }
124 }
125
126 static final class ConstantLoader<K, V> extends CacheLoader<K, V> {
127 private final V constant;
128
129 ConstantLoader(V constant) {
130 this.constant = constant;
131 }
132
133 @Override
134 public V load(K key) {
135 return constant;
136 }
137 }
138
139
140
141
142
143
144
145 static class IncrementingLoader extends CacheLoader<Integer, Integer> {
146 private final AtomicInteger countLoad = new AtomicInteger();
147 private final AtomicInteger countReload = new AtomicInteger();
148
149 @Override
150 public Integer load(Integer key) {
151 countLoad.incrementAndGet();
152 return key;
153 }
154
155 @GwtIncompatible("reload")
156 @Override
157 public ListenableFuture<Integer> reload(Integer key, Integer oldValue) {
158 countReload.incrementAndGet();
159 return Futures.immediateFuture(oldValue + 1);
160 }
161
162 public int getLoadCount() {
163 return countLoad.get();
164 }
165
166 public int getReloadCount() {
167 return countReload.get();
168 }
169 }
170
171 static final class IdentityLoader<T> extends CacheLoader<T, T> {
172 @Override
173 public T load(T key) {
174 return key;
175 }
176 }
177 }